<template>
    <div class="views-jiedantongji">
        <div>
            <el-form action="javascript:;" @submit="searchSubmit" inline>
                <el-form-item label="项目名称">
                    <el-input v-model="search.xiangmumingcheng"></el-input>
                </el-form-item>
                <el-form-item label="项目分类">
                    <el-select v-model="search.xiangmufenlei"
                        ><el-option label="请选择" value=""></el-option><e-select-option type="option" module="fuwufenlei" value="id" label="fenleimingcheng"></e-select-option
                    ></el-select>
                </el-form-item>
                <el-form-item label="添加时间">
                    <div style="display: flex">
                        <el-date-picker v-model="search.addtime_start" type="date" value-format="YYYY-MM-DD" placeholder="选择开始日期" />
                        -
                        <el-date-picker v-model="search.addtime_end" type="date" value-format="YYYY-MM-DD" placeholder="选择结束日期" />
                    </div> </el-form-item
            ></el-form>
        </div>
        <div>
            <e-container>
                <el-row :gutter="15">
                    <el-col :md="12">
                        <v3-echarts :options="barOptions" width="100%" height="300px"></v3-echarts>
                    </el-col>
                    <el-col :md="12">
                        <v3-echarts :options="barOptions1" width="100%" height="300px"></v3-echarts>
                    </el-col>
                </el-row>
            </e-container>
        </div>
        <div>
            <e-container>
                <div style="padding: 10px; background: #ffffff">
                    <el-table :data="jiedanList">
                        <el-table-column prop="fenleimingcheng" label="分类名称" width="180" />
                        <el-table-column prop="count" label="接单数量" sortable />
                    </el-table>
                </div>
            </e-container>
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";

    import { ref, reactive, watch, computed, unref } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { extend } from "@/utils/extend";

    const route = useRoute();

    const search = reactive({ xiangmumingcheng: "", xiangmufenlei: "", addtime_start: "", addtime_end: "" });
    extend(search, route.query);
    const serachSubmit = () => {
        loadDatabaseList();
    };
    var timer;
    watch(
        () => search,
        () => {
            clearTimeout(timer);
            timer = setTimeout(() => {
                serachSubmit();
            }, 200);
        },
        { deep: true }
    );
    const jiedanList = ref([]);
    const loadDatabaseList = async () => {
        var where = " 1=1 ";
        if (search.xiangmumingcheng) {
            where += ` AND jd.xiangmumingcheng LIKE '%${search.xiangmumingcheng}%' `;
        }
        if (search.xiangmufenlei) {
            where += ` AND jd.xiangmufenlei='${search.xiangmufenlei}' `;
        }
        if (search.addtime_start) {
            where += ` AND jd.addtime>='${search.addtime_start}' `;
        }
        if (search.addtime_end) {
            where += ` AND jd.addtime<='${search.addtime_end}' `;
        }
        jiedanList.value = await DB.name("jiedan")
            .alias("jd")
            .joinLeft("fuwufenlei fwf", "jd.xiangmufenlei=fwf.id")
            .field("fwf.fenleimingcheng")
            .group("fwf.fenleimingcheng")
            .field("count(jd.id) count")
            .where(where)
            .select();
    };

    loadDatabaseList();
    const barOptions = computed(() => {
        var list = unref(jiedanList);
        return {
            title: {
                text: "接单统计",
            },
            tooltip: {
                trigger: "axis",
                axisPointer: {
                    type: "shadow",
                },
            },
            grid: {
                left: "3%",
                right: "4%",
                bottom: "3%",
                containLabel: true,
            },
            xAxis: [
                {
                    type: "category",
                    data: list.map((s) => s.fenleimingcheng),
                    axisTick: {
                        alignWithLabel: true,
                    },
                },
            ],
            yAxis: [
                {
                    type: "value",
                },
            ],
            series: [
                {
                    name: "接单数量",
                    type: "bar",
                    barWidth: "20px",
                    data: list.map((s) => parseFloat(s.count)),
                },
            ],
        };
    });
    const barOptions1 = computed(() => {
        var list = unref(jiedanList);
        return {
            title: {
                text: "接单占比",
                left: "center",
            },
            tooltip: {
                trigger: "item",
                formatter: "{b}：{c},{d}%",
            },
            legend: {
                orient: "vertical",
                left: "left",
            },
            series: [
                {
                    name: "接单数量",
                    type: "pie",
                    radius: "50%",
                    data: list.map((s) => {
                        return {
                            value: parseFloat(s.count),
                            name: s.fenleimingcheng,
                        };
                    }),
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: "rgba(0, 0, 0, 0.5)",
                        },
                    },
                },
            ],
        };
    });
</script>

<style scoped lang="scss">
    .views-jiedantongji {
    }
</style>
